package com.wisemoresystem.seal.util.abchinaFinger.cics;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.wisemoresystem.seal.util.abchinaFinger.common.AbcCoreCfg;
import com.wisemoresystem.seal.util.abchinaFinger.common.CodeUtil;
import com.wisemoresystem.seal.util.abchinaFinger.common.PasswordUtil;

public class CoreTrans {
	
	private static Log log = LogFactory.getLog(CoreTrans.class);

	public String fingerCheck(String szProNo, String szBrno, String szTeller,
			String feature, String szDevFactoryID) {
		return fingerCheck(szProNo + szBrno + szTeller, feature, szDevFactoryID);

	}

	public String fingerCheck(String szTeller, String feature,
			String szDevFactoryID) {
		String szResult = "";
		if (szTeller.length() != 10)
			return "FAIL.柜员信息输入错误，格式{省代码}{行号}{柜员号}！";
		if (feature.length() != 343)
			return "FAIL.指纹信息输入错误！";
		if (!(szDevFactoryID.equals("WELLCOM")
				|| szDevFactoryID.equals("MIAXIS") || szDevFactoryID
					.equals("TECHSHINO")))
			return "FAIL.指纹仪厂家参数错！";
		try {
			AbcCoreCfg config = new AbcCoreCfg("AP26");
			CicsTrans cicsTran = new CicsTrans(config);// CICS通讯类
			FingerRequest fingerRequest = new FingerRequest();// 请求报文

			fingerRequest.setSzProNo(szTeller.substring(0, 2));
			fingerRequest.setSzBrno(szTeller.substring(2, 6));
			fingerRequest.setSzTeller(szTeller.substring(6, 10));

			fingerRequest.setSzDevFactoryID(szDevFactoryID);
			fingerRequest.setSzTransID(config.getTranid());

			fingerRequest.setSzDateTime(new java.text.SimpleDateFormat(
					"yyyyMMddHHmmss").format(new java.util.Date()));// 时间
			fingerRequest.setSzSingVal(feature);// 指纹信息
			/*
			 * if(ifmanager){ fingerRequest.setSzTransID("vs04");//管理员 }else{
			 * fingerRequest.setSzTransID("vs03");//柜员 }
			 */
			// 先尝试柜员指纹认证, 不成功时再尝试管理员指纹认证
			String[] szTransactionIds = { "vs03", "vs04" }; // vs03-柜员 vs04-管理员
			for (String szTransactionId : szTransactionIds) {
				fingerRequest.setSzTransID(szTransactionId);
				cicsTran.openGateway();
				log.debug("指纹认证数据：" + fingerRequest.toString());
				byte[] rsp1 = cicsTran
						.runECICall(fingerRequest.toString(), "0");
				String returnStr = new String(rsp1, "GBK");
				log.debug("指纹认证返回：" + returnStr);
				if ("0306".equals(returnStr.substring(543, 547))) {
					log.error("指纹认证失败：柜员号与网点不匹配");
					return "FAIL.指纹认证失败：柜员号与网点不匹配";
				} else if ("0308".equals(returnStr.substring(543, 547))) {
					log.error("指纹认证失败：指纹比对失败");
					return "FAIL.指纹认证失败：指纹比对失败";
				} else if ("100000000".equals(returnStr.substring(544, 553))) {
					log.debug("指纹认证完成！");
					return "SUCCESS.指纹认证成功";
				} else {
					log.error("指纹认证失败,服务器发生未知错误:["
							+ returnStr.substring(400).trim() + "]");
					return "FAIL.指纹认证失败,服务器发生未知错误";
				}
			}
		} catch (Exception e) {
			szResult = "FAIL.指纹认证失败：指纹服务器异常，请重新认证.";
			log.error("指纹认证出错", e);
			e.printStackTrace();
		}
		return szResult;
	}

	public String passwordCheck(String szProNo, String szBrno, String szTeller,
			String password) {
		return passwordCheck(szProNo + szBrno + szTeller, password);
	}

	public String passwordCheck(String szTeller, String password) {
		String szResult = "";
		try {
			AbcCoreCfg config = new AbcCoreCfg("ABIS");
			CicsTrans cicsTran = new CicsTrans(config);
			PasswordRequest passwordRequest = new PasswordRequest();
			passwordRequest.setSzProNo(szTeller.substring(0, 2));// 省号teller.substring(0,2)
			passwordRequest.setSzTeller(szTeller.substring(6, 10));// 柜员号
																	// teller.substring(6,8)
			passwordRequest.setSzSysTerm(config.getTerm());
			passwordRequest.setSzSysTeller(config.getTeller());
			passwordRequest.setSzPassword(PasswordUtil.transform(password));// password
			log.debug("密码认证数据：" + passwordRequest.toString());
			cicsTran.openGateway();
			byte[] rsp = cicsTran.runECICall(passwordRequest.toString(), "1");
			rsp = CodeUtil.EBCDICToASCII(rsp);
			String returnStr = new String(rsp, "GBK");
			log.debug(returnStr);
			String result = returnStr.substring(60, 61);
			// 登录成功
			if (result.equals("N")) {
				log.debug("密码认证成功！");
				szResult = "SUCCESS.密码认证成功";

			} else if (result.equals("E")) {
				String msg = returnStr.substring(106, 110);
				// 登录失败
				if (msg.equals("1088")) {
					log.error("密码认证失败：密码错误");
					szResult = "FAIL.密码错误";
				} else if (msg.equals("1152")) {
					log.error("密码认证失败:用户不存在");
					szResult = "FAIL.用户不存在";
				} else {
					log.error("密码认证失败:服务器发生未知错误，错误码" + msg);
					szResult = "FAIL.服务器发生未知错误，错误码[" + msg + "]";
				}
			} else {
				log.error("密码认证失败:服务器发生未知错，请联系管理员");
				szResult = "FAIL.服务器发生未知错，请联系管理员";
			}
		} catch (Exception e) {
			log.error("密码认证失败：密码服务器异常，请重新认证.");
			e.printStackTrace();
			szResult = "FAIL.密码服务器异常，请重新认证.";
		}
		return szResult;
	}

}
